package com.qq.BFMRSE.requestModule;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import net.sf.json.JSONArray;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;

import com.qq.BFMRSE.entity.BloomFilter;
import com.qq.BFMRSE.entity.indexTree;
import com.qq.BFMRSE.entity.indexTreeNode;
import com.qq.BFMRSE.entity.reposeParam;
import com.qq.BFMRSE.extractWord.luIndexUtil;
import com.qq.BFMRSE.extractWord.updateLucencHelper;
import com.qq.BFMRSE.index.buildIndexTreeHelper;
import com.qq.BFMRSE.serialize.objectSerializeUtil;
import com.qq.BFMRSE.util.BFConstants;

public class deleteFileModule {
	private String userName;
	private String deleteFileName;
	private String indexPath;
	public String getIndexPath() {
		return indexPath;
	}
	public void setIndexPath(String indexPath) {
		this.indexPath = indexPath;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getDeleteFileName() {
		return deleteFileName;
	}
	public void setDeleteFileName(String deleteFileName) {
		this.deleteFileName = deleteFileName;
	}
	public deleteFileModule(String userName,String deleteFileName,String indexPath){
		this.userName=userName;
		this.deleteFileName=deleteFileName;
		this.indexPath=indexPath;
	}
	public deleteFileModule(){}
	public reposeParam deleteFile(String url) throws IOException{
		System.out.println("test:deleteFile exec");
		reposeParam resParam=new reposeParam();
		String rootPath=BFConstants.INDEXPATH+File.separator+BFConstants.ROOT;
		indexTreeNode root=(indexTreeNode) objectSerializeUtil.deserialize(rootPath);
		if(root!=null){
			indexTree searchTree=new indexTree(root);
			indexTreeNode deleteNode=searchTree.getLeafNodeByFileName(root, deleteFileName);
			deleteNode.setBf(new BloomFilter());
			deleteNode.setFID("");//将该节点设置为空节点
			buildIndexTreeHelper.updateIndexTree(deleteNode);//更新树的bf值
			objectSerializeUtil.serialize(root, rootPath);//将更新后的索引树序列化
			Stack<Integer> pathStack=searchTree.getPathToChangedNode(root, deleteNode);//找到根节点到要删除的叶节点的路径
			JSONArray ja=JSONArray.fromObject(pathStack);//路径信息
			String emptyNodesPath=BFConstants.INDEXPATH+File.separator+BFConstants.EMPTYNODES;
			List<Stack<Integer>> emptyNodePaths=(List<Stack<Integer>>) objectSerializeUtil.deserialize(emptyNodesPath);
			if(emptyNodePaths==null)
			{
				emptyNodePaths=new ArrayList<Stack<Integer>>();
			}
			emptyNodePaths.add(pathStack);//更新空节点信息
			objectSerializeUtil.serialize(emptyNodePaths, emptyNodesPath);//将更新后的空节点路径信息序列化
			luIndexUtil.deleteLuIndex(indexPath, deleteFileName);//更新本地lucene索引
			HttpClient httpClient=new HttpClient();
			PostMethod post=new PostMethod(url);
			post.setParameter("path", ja.toString());
			post.setParameter("fileName", deleteFileName);
			post.setParameter("userName", userName);
			try {
				httpClient.executeMethod(post);
				BufferedReader br=new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));
				String flag=br.readLine();
				br.close();
				resParam.setFlag(Boolean.parseBoolean(flag.trim()));
				resParam.setStateCode(post.getStatusCode());
				//stateCode=post.getStatusCode();
			} catch (HttpException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
			resParam.setFlag(false);
			resParam.setStateCode(-1);
		}
	
		return resParam;
	}
	

}
